package face.pay.util;

import lombok.extern.slf4j.Slf4j;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 提现工具类
 */
@Slf4j
public class WithdrawUtil {



    /**
     * 对象转换成xml
     *
     * @param obj
     * @return
     */
    public static String object2xml(Object obj) {
        String xmlObj = null;
        try {
            JAXBContext context = JAXBContext.newInstance(obj.getClass());
            Marshaller marshaller = context.createMarshaller();

            marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            marshaller.marshal(obj, baos);
            xmlObj = new String(baos.toByteArray());
        } catch (JAXBException e) {
            log.error("object2xml异常" + e.toString());
        }
        return xmlObj;
    }




    /**
     * xml转成map参数
     * @param xml
     * @return
     */
    public static Map<String, String> xmlToMap(String xml){

        Map<String, String> respMap = null;
        try {
            respMap = new HashMap<String, String>();

            SAXReader reader = new SAXReader();
            Document doc = reader.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));
            Element root = doc.getRootElement();
            xmlToMap(root, respMap);
        } catch (Exception e) {
            log.error("xmlToMap解析异常"+e.toString());
        }
        return respMap;
    }

    public static Map<String, String> xmlToMap(Element tmpElement, Map<String, String> respMap){

        if (tmpElement.isTextOnly()) {
            respMap.put(tmpElement.getName(), tmpElement.getText());
            return respMap;
        }

        @SuppressWarnings("unchecked")
        Iterator<Element> eItor = tmpElement.elementIterator();
        while (eItor.hasNext()) {
            Element element = eItor.next();
            xmlToMap(element, respMap);
        }
        return respMap;
    }







    public static void main(String[] args) throws UnsupportedEncodingException, DocumentException {
        String str = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n" +
                "<yemadai>\n" +
                "\t<accountNumber>17005</accountNumber>\n" +
                "\t<notifyURL>http://notify.merchant.com</notifyURL>\n" +
                "\t<tt>0</tt>\n" +
                "\t<signType>RSA</signType>\n" +
                "\t<transferList>\n" +
                "\t\t<transId>test10001</transId>\n" +
                "\t\t<bankCode>工商银行</bankCode>\n" +
                "\t\t<provice>北京市</provice>\t\t --代表北京市\n" +
                "\t\t<city>北京市</city>\t\t --代表北京市\n" +
                "\t\t<branchName>平江路支行</branchName>\n" +
                "\t\t<accountName>王五</accountName>\n" +
                "         <idNo>150624197307108592</idNo>\n" +
                "         <phone>17555551234</phone>\n" +
                "\t\t<cardNo>6222021001067998889</cardNo>\n" +
                "\t\t<amount>10.00</amount>\n" +
                "\t\t<remark>测试转账</remark>\n" +
                "\t\t<secureCode>A13230D0CBFD964621B26984D513D13F</secureCode>\n" +
                "\t</transferList>\n" +
                "</yemadai>";
        Map<String, String> map = WithdrawUtil.xmlToMap(str);
        System.out.println("map = " + map);


    }

}
